package com.king.java.tools.excel.easy.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.king.java.tools.excel.CaseResult;
import com.king.java.tools.excel.entity.CaseData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述: 用例数据的读取监听器
 *
 * @author : sujinchen
 * @date : 2020/11/3
 */
public class CaseDataLinstener extends AnalysisEventListener<CaseData> implements CaseResult {
    private static final Logger LOGGER = LoggerFactory.getLogger(CaseDataLinstener.class);
    /**
     * 每隔5条存储数据库，实际使用中可以3000条，然后清理list ，方便内存回收
     */
    private static final int BATCH_COUNT = 5;

    private List<CaseData> pass = new ArrayList<>();
    private List<CaseData> fail=new ArrayList<>();
    private List<CaseData> ToDo=new ArrayList<>();
    private List<CaseData> NA=new ArrayList<>();
    private List<CaseData> empty=new ArrayList<>();
    private int total = 0;
    /**
     * 这个每一条数据解析都会来调用
     * @param data
     * @param analysisContext
     */
    @Override
    public void invoke(CaseData data, AnalysisContext analysisContext) {
        LOGGER.info("解析到一条数据:{}", data);
        total++;
        if (data.getResult() == null) {
                empty.add(data);
        }else {
            String result = data.getResult();
            if (result.equals("ToDo")) {
                ToDo.add(data);
            }
            if (result.equals("N/A")) {
                NA.add(data);
            }
            if (result.equals("Pass")) {
                pass.add(data);
            }
            if (result.equals("Fail")) {
                fail.add(data);
            }
        }


    }

    @Override
    public void doAfterAllAnalysed(AnalysisContext analysisContext) {
        LOGGER.info("所有数据解析完成！");
    }

    @Override
    public List<CaseData> getPass() {
        return pass;
    }

    @Override
    public List<CaseData> getFail() {
        return fail;
    }

    @Override
    public List<CaseData> getToDo() {
        return ToDo;
    }

    @Override
    public List<CaseData> getNA() {
        return NA;
    }

    @Override
    public List<CaseData> getEmpty() {
        return empty;
    }

    @Override
    public Map<String, Integer> show() {
        Map<String, Integer> map = new HashMap<>();
        map.put("total", total);
        map.put("NA", NA.size());
        map.put("empty", empty.size());
        map.put("Pass", pass.size());
        map.put("Fail", fail.size());
        map.put("ToDo", ToDo.size());
        return map;
    }
}
